﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Text;

namespace Chaunce.FrameWork.NetCore.DapperCore
{
    public class DbConnectionFactory
    {
        private readonly DbProviderFactory _provider;
        private readonly string _connectionString;
        private readonly string _name;


        public DbConnectionFactory(string connectionStringName, DataProvider dataProvider)
        {
            if (connectionStringName == null) throw new ArgumentNullException("connectionStringName");

            _provider = DbProviderFactories.GetFactory(dataProvider);
            _connectionString = connectionStringName;
        }

        /// <summary>
        /// Creates a new instance of <see cref="IDbConnection"/>.
        /// </summary>
        /// <exception cref="ConfigurationErrorsException">Thrown if the connectionstring entry in the app/web.config file is missing information, contains errors or is missing entirely.</exception>
        /// <returns></returns>
        public IDbConnection Create()
        {
            var connection = _provider.CreateConnection();

            //if (connection == null)
            //    throw new ConfigurationErrorsException(
            //        string.Format(
            //            "Failed to create a connection using the connection string named '{0}' in app.config or web.config.",
            //            _name));

            connection.ConnectionString = _connectionString;
            return connection;
        }
    }
}
